<?php

namespace App\Controller\Api;

use App\Base\BaseController4Restful;
use App\Entity\Article;
use App\Repository\ArticleCategoryRepository;
use App\Repository\ArticleRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use function Doctrine\ORM\QueryBuilder;

class ApiArticleController extends BaseController4Restful
{
    /**
     * @Route("/api/articles", name="apiGetArticles",methods={"GET","POST"})
     */
    public function apiGetArticles(ArticleRepository $repository,Request $request,ArticleCategoryRepository $categoryRepository)
    {
        switch ($request->getMethod()) {
            case "POST":
                $data = $this->getRequestData();
                $data["view"] *=1;
                /**@var $Article Article*/
                $Article = $this->createEntity($data,Article::class);
                $this->myEm()->persist($Article);$this->myEm()->flush();
                return $this->json_out(200,"创建成功", $Article);
            default:
                if (!$page = $this->getParamRequired("page")) {$page = 1;}
                if(!$limit = $this->getParamRequired("limit")){$limit = 20;}
                if(!$cat = $this->getParamRequired("cat")){
                    $cond["cat"] = [];
                }else{
                    $cond["cat"] = $categoryRepository->find($cat);
                }

                $offset = (int)abs($limit * ($page - 1));
//                $total = $repository->count($cond);
//                $this->setMeta($page,$total,$limit);
//                $list = $repository->findBy($cond,array("createAt"=>"DESC"),$limit,$offset);

                $qb = $repository->createQueryBuilder("A");
                $qb->where("A.id > 0");
                if ($cond["cat"]) {
                    $qb->andWhere("A.cat = {$cat}");
                }
                if($keywords = $this->getParamRequired("keywords")){
                    $qb->andWhere(
                        $qb->expr()->orX(
                            $qb->expr()->like("A.title","'%{$keywords}%'"),
                            $qb->expr()->like("A.intro","'%{$keywords}%'"),
                            $qb->expr()->like("A.content","'%{$keywords}%'")
                        )
                    );
                }
                $qb->select("count(A.id) as num");
                $total = $qb->getQuery()->execute()[0]["num"];
                $this->setMeta($page,$total,$limit);
                $qb = $repository->createQueryBuilder("A");
                $qb->where("A.id > 0");
                if ($cond["cat"]) {
                    $qb->andWhere("A.cat = {$cat}");
                }
                if($keywords = $this->getParamRequired("keywords")){
                    $qb->andWhere(
                        $qb->expr()->orX(
                            $qb->expr()->like("A.title","'%{$keywords}%'"),
                            $qb->expr()->like("A.intro","'%{$keywords}%'"),
                            $qb->expr()->like("A.content","'%{$keywords}%'")
                        )
                    );
                }
                $qb->select("A");
                $qb->setMaxResults($limit)->setFirstResult($offset);
                $qb->orderBy("A.createAt","DESC");
                $list = $qb->getQuery()->execute();




                return $this->json_out(200,"获取成功", $list);
        }
    }

    /**
     * @Route("/api/articles/{id}", name="apiAddArticles",methods={"GET","PUT","PATCH","DELETE"})
     */
    public function apiUpdateArticles(Article $Article,Request $request)
    {
        $data = $this->getRequestData();
        switch ($request->getMethod()) {
            case "DELETE":
                $this->myEm()->remove($Article);$this->myEm()->flush();
                return $this->json_out(200,"已移除");
            case "PATCH":
            case "PUT":
                $data["view"] *=1;
                /**@var $Article Article*/
                $Article = $this->updateEntity($data,Article::class,$Article);
                break;
            case "Get":
            default:
        }
        $this->myEm()->persist($Article);$this->myEm()->flush();
        return $this->json_out(200,"获取成功", $Article);
    }
}
